package com.itsfoodme.login;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;

public class Geolocation{
	private Location geo;
	//private Location geo1;
	private boolean stop = false;
	public Location GetLocation(final Activity A) {
		LocationManager locationManager = (LocationManager) A.getSystemService(Context.LOCATION_SERVICE);

        //geo1 = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
            	geo = location;
            	Log.i("Location class",String.valueOf(geo));
            	stop = true;
            }
            public void onStatusChanged(String provider, int status, Bundle extras) {}

            public void onProviderEnabled(String provider) {}

            public void onProviderDisabled(String provider) {
            	Log.i("Location class","gps not enabled");
            	AlertDialog.Builder builder = new AlertDialog.Builder(A);
            	builder.setMessage("Deseas activar tu GPS para Fijar tu Pais Automaticamente?")
            	       .setCancelable(false)
            	       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            	           public void onClick(DialogInterface dialog, int id) {
            	        	   Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            	            	A.startActivity(intent);
            	           }
            	       })
            	       .setNegativeButton("No", new DialogInterface.OnClickListener() {
            	           public void onClick(DialogInterface dialog, int id) {
            	                dialog.cancel();
            	           }
            	       });
            	AlertDialog alert = builder.create();
            	alert.show();
            }
          };

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
        
        if(stop){
        	// Remove the listener you previously added
        	locationManager.removeUpdates(locationListener);
        }
		return geo;
	}
}
